/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.starlink.context;

import com.starlink.common.domain.BaseVO;
import com.starlink.model.vo.AdminVO;
import com.starlink.model.vo.UserVO;

import java.util.Optional;

/**
 * @author zhanghong
 * @version SessionContext.java, v 0.1 2023/6/21 17:54 下午 SessionContext Exp $$
 */
public class SessionContext {
    public static ThreadLocal<BaseSession> context = new ThreadLocal<>();

    /**
     * 获取当前登陆用户信息
     * @return
     */
    public static Long getLoginAccountId() {
        BaseSession baseSession = context.get();
        if (baseSession == null) {
            return null;
        }

        AdminVO adminVO = baseSession.getAdminVO();
        if (adminVO != null) {
            return adminVO.getId();
        }

        UserVO userVO = baseSession.getUserVO();
        if (userVO != null) {
            return userVO.getId();
        }
        return null;
    }

    /**
     * 获取管理端登陆用户信息
     * @return
     */
    public static AdminVO getAdminUserInfo() {
        BaseSession baseSession = context.get();
        if (baseSession == null) {
            return null;
        }
        return baseSession.getAdminVO();
    }

    /**
     * 获取管理端登录用户id
     * @return
     */
    public static Long getAdminUserId() {
        BaseSession baseSession = context.get();
        if (baseSession == null) {
            return null;
        }
        return Optional.ofNullable(baseSession.getAdminVO()).map(BaseVO::getId).orElse(null);
    }

    /**
     * 获取小程序端登陆用户信息
     * @return
     */
    public static UserVO getWechatUserInfo() {
        BaseSession baseSession = context.get();
        if (baseSession == null) {
            return null;
        }
        return baseSession.getUserVO();
    }

    /**
     * 获取小程序端登录用户id
     * @return
     */
    public static Long getWechatUserId() {
        BaseSession baseSession = context.get();
        if (baseSession == null) {
            return null;
        }
        return Optional.ofNullable(baseSession.getUserVO()).map(BaseVO::getId).orElse(null);
    }
}
